﻿using AsyncAwaitBestPractices.MVVM;
using ImTools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using RestSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using Wemail.Base;
using Wemail.Common.Http;
using Wemail.Models;
using Wemail.Views;

namespace Wemail.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
  
        private string _searchText;
        public string SearchText { get { return _searchText; } set { SetProperty(ref _searchText, value); } }
        private string _title = "Prism Application";
        private AsyncCommand _searchCommand;
        public AsyncCommand SearchCommand { get => _searchCommand = new AsyncCommand(MyMethod); }
        public ObservableCollection<Novel> DataCollection { get; set; }
        public ObservableCollection<ChapterListAnaCommand> ChapterList { get; set; }
        private string _content;
        public string Content { get { return _content; } set { SetProperty(ref _content, value); } }

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

      // 通过依赖注入获取 IEventAggregator 实例
        public MainWindowViewModel(IRegionManager regionManager,IEventAggregator eventAggregator)
        { 
            ChapterList = new ObservableCollection<ChapterListAnaCommand>();
            DataCollection=new ObservableCollection<Novel>();
            Content = "";
            DataCollection.Add(new Novel()
            {
                fictionId = "e8bc8dd5-8877-3da6-ac96-3ede6ed61834",
                title = "全职高手",
                author = "蝴蝶蓝",
                fictionType = "网游小说",
                descs = "网游荣耀中被誉为教科书级别的顶尖高手，因为种种原因遭到俱乐部的驱逐，离开职业圈的他寄身于一家网吧成了一个小小的网管，但是，拥有十年游戏经验的他，在荣耀新开的第十区重新投入了游戏，带着对往昔的回忆，和一把未完成的自制武器，开始了重返巅峰之路。",
                cover = "http://api.pingcc.cn/fiction/img/全职高手/全职高手.jpg",
                updateTime = "2015-04-20 00:00:00",
                command=new AsyncCommand(async ()=> await GetChapters("e8bc8dd5-8877-3da6-ac96-3ede6ed61834")),
            });
            DataCollection.Add(new Novel()
            { 
                fictionId = "c617157f-33d0-3f23-8e71-3c28febe84c7",
                title = "近身全职高手",
                author = "封家小少作品集",
                fictionType = "武侠修真",
                descs = "兵王李晓东回归都市却意外成了美女董事长的贴身保镖，飞刀绝技，出神入化的功夫，惹来无数美女垂涎，游戏花丛，笑傲人生。分享书籍《近身全职高手》作者：封家小少作品集......",
                cover = "http://api.pingcc.cn/fiction/img/近身全职高手/近身全职高手.jpg",
                updateTime = "2015-03-27 00:00:00",
                command=new AsyncCommand(async ()=> await GetChapters("c617157f-33d0-3f23-8e71-3c28febe84c7")),
            });
             
        }


        public async Task  MyMethod()
        {
          
            try
            {   
                string responseContent = await RestHelper.MakeHttpRequestAsync(SearchText);
                SearchText = "";
              
                var responseObject = JsonConvert.DeserializeObject<Response>(responseContent);
                var response = responseObject.data;
                ChapterList.Clear();
                DataCollection.Clear();
                for(int i = 0; i < response.Count; i++)
                {
                    DataCollection.Add(new Novel()
                    {
                        fictionId = response[i].fictionId,
                        title = response[i].title,
                        author = response[i].author,
                        cover = response[i].cover,
                        updateTime = response[i].updateTime,
                        descs = response[i].descs,
                        fictionType = response[i].fictionType,
                        command = null,

                    });
                }
                foreach (var item in DataCollection)
                {
                    item.command = new AsyncCommand(async () =>await GetChapters(item.fictionId));

                        };
              
                // 在这里处理响应内容

            }
            catch (HttpRequestException ex)
            {
                // 处理请求异常的逻辑
                Console.WriteLine("HTTP 请求发生错误：" + ex.Message);
            }
        }


        public async Task GetChapters(string parameter)
        {    
            try
            {   
                string responseContent = await RestHelper.GetChapter(parameter);
                
                //映射对象
                var responseObject = JsonConvert.DeserializeObject<GetChapter>(responseContent);
                var response = responseObject.data.chapterList;

                ChapterList.Clear();
                for (int i = 0; i < response.Count; i++)
                {
                    ChapterList.Add(new ChapterListAnaCommand()
                    {
                        title = response[i].title,
                        chapterId = response[i].chapterId,
                        cmd=null,
                       
                    });
                }

                foreach(var item in ChapterList)
                {
                   
                        item.cmd = new AsyncCommand(async () => await GetContent(item.chapterId));
                }
             

            }
            catch (HttpRequestException ex)
            {
                // 处理请求异常的逻辑
                Console.WriteLine("HTTP 请求发生错误：" + ex.Message);
            }
        }
         public async Task GetContent(string parameter)
        {    
            try
            {   
                string responseContent = await RestHelper.GetContent(parameter);
                
                //映射对象
                var responseObject = JsonConvert.DeserializeObject<GetContent>(responseContent);
                var response = responseObject.data;
                Content = "";
                for(int i = 0;i < response.Count; i++)
                {
                    Content = Content + response[i];
                }
               
              


                // 在这里处理响应内容

            }
            catch (HttpRequestException ex)
            {
                // 处理请求异常的逻辑
                Console.WriteLine("HTTP 请求发生错误：" + ex.Message);
            }
        }



    }
}
